/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.im.controller;

import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.User;
import com.yami.shop.im.bean.ImRecord;
import com.yami.shop.im.bo.ShopSessionInfo;
import com.yami.shop.im.bo.SysSessionInfo;
import com.yami.shop.im.bo.UserSessionInfo;
import com.yami.shop.im.param.SysUserParam;
import com.yami.shop.im.service.ImRecordService;
import com.yami.shop.im.util.SecurityUtils;
import com.yami.shop.im.vo.Conversation;
import com.yami.shop.im.vo.MsgItem;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/p/user")
@Api(tags = "用户聊天接口")
@AllArgsConstructor
public class UserController {


    @Autowired
    private ImRecordService imRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopDetailService shopDetailService;


    @GetMapping("/conversations")
    @ApiOperation(value = "最后的对话信息列表项", notes = "获取最后的对话信息列表项")
    public ResponseEntity<List<Conversation>> conversations() {
        YamiUser user = SecurityUtils.getUser();
        // 3天内的聊天记录
        List<Conversation> conversations = imRecordService.listConversationByUserId(user.getUserId(), 3);
        return ResponseEntity.ok(conversations);
    }

    @GetMapping("/msgItems/{shopId}")
    @ApiOperation(value = "和某人的聊天历史记录", notes = "获取和某人的聊天历史记录")
    public ResponseEntity<List<MsgItem>> msgItems(@PathVariable("shopId") Long shopId) {
        YamiUser user = SecurityUtils.getUser();
        User dbUser = userService.getUserByUserId(user.getUserId());
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        UserSessionInfo userSessionInfo = new UserSessionInfo();
        userSessionInfo.setUserId(user.getUserId());
        userSessionInfo.setPic(dbUser.getPic());
        userSessionInfo.setNickName(dbUser.getNickName());
        ShopSessionInfo shopSessionInfo = new ShopSessionInfo();
        shopSessionInfo.setShopName(shopDetail.getShopName());
        shopSessionInfo.setShopId(shopDetail.getShopId());
        shopSessionInfo.setPic(shopDetail.getShopLogo());
        List<ImRecord> imRecords = imRecordService.listHistory(shopId, user.getUserId(), 20, 2);

        List<MsgItem> msgItems = new ArrayList<>();
        for (ImRecord record : imRecords) {
            MsgItem msgItem = imRecordService.getMsgItem(shopSessionInfo, userSessionInfo, record);
            msgItems.add(msgItem);
        }
        // 看了和某人的聊天记录，将未读聊天记录变成已读
        imRecordService.updateToRead(user.getUserId(), shopId, 1, 2);

        return ResponseEntity.ok(msgItems);
    }

    @GetMapping("/msgItemsPlatform/{shopId}")
    @ApiOperation(value = "和某人的平台聊天历史记录", notes = "获取和某人的平台聊天历史记录")
    public ResponseEntity<List<MsgItem>> msgItemsPlatform(@PathVariable("shopId") Long shopId) {
        YamiUser user = SecurityUtils.getUser();
        User dbUser = userService.getUserByUserId(user.getUserId());
        UserSessionInfo userSessionInfo = new UserSessionInfo();
        userSessionInfo.setUserId(user.getUserId());
        userSessionInfo.setPic(dbUser.getPic());
        userSessionInfo.setNickName(dbUser.getNickName());
        List<MsgItem> msgItems = new ArrayList<>();
        SysUserParam userParam = imRecordService.getLatelyUser(3,user.getUserId());
        // 如果shopId为0并且最近的平台聊天记录的平台用户也为空，则返回空
        if(shopId == 0 && Objects.isNull(userParam)){
            return ResponseEntity.ok(msgItems);
        }
        shopId = userParam.getUserId();
        List<ImRecord> imRecords = imRecordService.listHistory(shopId, user.getUserId(), 20, 1);
        SysSessionInfo sysSessionInfo = new SysSessionInfo();
        sysSessionInfo.setUserName(userParam.getUserName());
        sysSessionInfo.setUserId(userParam.getUserId());
        for (ImRecord record : imRecords) {
            MsgItem msgItem = imRecordService.getMsgItemByPlatform(sysSessionInfo, userSessionInfo, record);
            msgItems.add(msgItem);
        }
        // 看了和某人的聊天记录，将未读聊天记录变成已读
        imRecordService.updateToRead(user.getUserId(), shopId, 1, 1);

        return ResponseEntity.ok(msgItems);
    }



}
